package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.wondertek.oes.sso.common.util.StringUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.*;
import com.wondertek.poms.dao.po.*;
import com.wondertek.poms.dao.vo.ProductChargeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p><b>PrdContChargeServiceImpl Description:</b> 批价业务类</p>
 * @author baijiawei
 * <b>DATE</b> 2020年12月8日 下午4:02:46
 */
@Service
@Slf4j
public class PrdContChargeServiceImpl extends ServiceImpl<PrdContCharge, Long> implements IPrdContChargeService {

	@Autowired
	private IContentFeeService contentFeeServiceImpl;

	@Autowired
	private IProductService productServiceImpl;
	@Autowired
	private IEquityNodeService equityNodeService;
	@Autowired
	private IProductGroupService productGroupService;
	@PersistenceContext
    public EntityManager entityManager;

    @SuppressWarnings("unchecked")
	@Override
	public Page<PrdContCharge> findAll(PrdContCharge prdContCharge, int page, int rows, String sord, String sidx) {
		StringBuilder condition = sqlStringWhere(prdContCharge);
		Object o = entityManager.createNativeQuery("select count(*) from ( " + condition.toString() + " ) a")
				.getSingleResult();
		int count = Convert.toInt(o);
		Pageable pageable = getPageable(page - 1, rows, sord, sidx);

		if (count > 0) {
			Query query = entityManager.createNativeQuery(condition.toString(), PrdContCharge.class);
			query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
			query.setMaxResults(pageable.getPageSize());
			List<PrdContCharge> result = query.getResultList();
			PageImpl<PrdContCharge> list = new PageImpl<>(result, pageable, count);
			entityManager.close();
			return list;
		}
		entityManager.close();
		return Page.empty(pageable);
	}

	@Override
	public List<ProductChargeVo> prdSalList(String type,String prdContIds) {
		List<ContentFee> list = new ArrayList<ContentFee>();
		List<ProductChargeVo> list1 = new ArrayList<ProductChargeVo>();
		String[] ids = prdContIds.split(",");
		for (String id : ids) {
			list = contentFeeServiceImpl.findByTypeAndPomsId(Integer.parseInt(type),Long.parseLong(id));
			log.info(JSON.toJSONString(list));
			if (list.size() > 0) {
				for (ContentFee fee : list) {
					Product product = null;
					if (!StringUtil.isNullStr(fee.getSaleProductId())) {
						product = productServiceImpl.findById(Long.parseLong(fee.getSaleProductId()));
					}
					if (!StringUtil.isNullStr(fee.getEquityId())) {
						ProductChargeVo productCharge = new ProductChargeVo();
						productCharge.setId(fee.getId().toString());
						productCharge.setPrdContId(fee.getPomsId().toString());
						productCharge.setEquityId(fee.getEquityId());
						EquityNode equityNode = equityNodeService.findById(StringUtil.stringToLong(fee.getEquityId()));
						productCharge.setEquityCode(equityNode.getEquityNodeId());
						ProductGroup productGroup = productGroupService.findById(equityNode.getGroupId());
						if (productGroup != null) {
							productCharge.setGroupName(productGroup
									.getGroupName());
						}
						productCharge.setStartTime(fee.getStartTime());
						productCharge.setEndTime(fee.getEndTime());
						list1.add(productCharge);
					} else if (!StringUtil.isNullStr(fee.getDiscount())) {
						if (product != null) {
							ProductChargeVo productCharge = new ProductChargeVo();
							productCharge.setName(product.getName());
							productCharge.setPrice(product.getPrice());
							productCharge.setPrdContId(fee.getPomsId()
									.toString());
							productCharge.setChargeProductId(product.getProductID());
							productCharge.setChargeMode(product.getChargeMode());
							productCharge.setStartTime(fee.getStartTime());
							productCharge.setEndTime(fee.getEndTime());
							productCharge.setSalCategory(fee.getSaleCategory());
							productCharge.setId(fee.getId().toString());
							productCharge.setDiscount(fee.getDiscount());
							productCharge.setChannelPackageId(fee
									.getChannelPackageId() == null ? null : fee
									.getChannelPackageId().toString());
							productCharge
									.setClientType(fee.getClientType() == null ? null
											: fee.getClientType().toString());
							list1.add(productCharge);
						}
					} else {
						ProductChargeVo productCharge = new ProductChargeVo();
						productCharge.setPrdContId(fee.getPomsId().toString());
						productCharge.setId(fee.getId().toString());
						assert product != null;
						productCharge.setChargeProductId(product.getProductID());
						productCharge.setPrice(fee.getPrice());
						productCharge.setName(product.getName());
						productCharge.setStartTime(fee.getStartTime());
						productCharge.setEndTime(fee.getEndTime());
						list1.add(productCharge);
					}
				}
			}
		}
		return null;
	}

	public StringBuilder sqlStringWhere(PrdContCharge prdContCharge) {
		StringBuilder condition = new StringBuilder();
		String sql = "select * from PRD_CONT_CHARGE where 1=1 ";
		condition.append(sql);

		if (StrUtil.isNotBlank(prdContCharge.getPrdContId())) {
			condition.append("and PRD_CONT_ID = " + prdContCharge.getPrdContId() + " ");
		}
		if (StrUtil.isNotBlank(prdContCharge.getPrdInfoId())) {
			condition.append(" and PRD_INFO_ID = " + prdContCharge.getPrdInfoId() + " ");
		}
		if (StrUtil.isNotBlank(prdContCharge.getPrdContName())) {
			condition.append(" and PRD_CONT_NAME like '%" + prdContCharge.getPrdContName() + "%' ");
		}
		if (StrUtil.isNotBlank(prdContCharge.getChargeId())) {
			condition.append(" and CHARGE_ID = " + prdContCharge.getChargeId() + " ");
		}
		if (StrUtil.isNotBlank(prdContCharge.getProductProp())) {
			condition.append(" and PRODUCT_PROP = '" + prdContCharge.getProductProp() + "' ");
		}
		if (StrUtil.isNotBlank(prdContCharge.getStatus())) {
			condition.append(" and STATUS=" + prdContCharge.getStatus() + " ");
		}
		return condition;
	}

    @Override
    public PrdContCharge deleteLogic(Long id) {
        return null;
    }

    @Override
    public PrdContCharge merge(PrdContCharge model) {
        return null;
    }
}
